
#
#  终端执行 bash shell_build_framework.sh PPCommonUI
#

#当前项目所在路径
current_path=`pwd`

#项目
workspace_name="$1.xcworkspace"
target_name="$1"
framework_config="Release"

#framework输出路径
framework_target_dir=$current_path/"Products"

#临时文件夹路径
build_dir_tem=$current_path/"FrameworkTmp"

echo "当前项目所在路径:$current_path"
echo "临时文件夹:$build_dir_tem"
echo "framework输出路径:${framework_target_dir}"

buildWorkSpace(){
#build之前清理文件
xcodebuild clean -workspace ${workspace_name} -scheme ${target_name}
#模拟器build
xcodebuild build -workspace ${workspace_name} -scheme ${target_name} -configuration framework_config -sdk iphonesimulator SYMROOT=${build_dir_tem}
#真机build
xcodebuild build -workspace ${workspace_name} -scheme ${target_name} -configuration framework_config -sdk iphoneos SYMROOT=${build_dir_tem}
#OBJROOT=${build_dir_tem} SYMROOT=${build_dir_tem}

#将模拟器中的framework复制出来
cp -R ${build_dir_tem}/${framework_config}-iphonesimulator/${target_name}.framework ${framework_target_dir}

#从模拟器中移除arm64架构
lipo ${build_dir_tem}/${framework_config}-iphonesimulator/${target_name}.framework/${target_name} -remove arm64 -output ${build_dir_tem}/${framework_config}-iphonesimulator/${target_name}.framework/${target_name}
#合并真机和模拟器文件并输出到构建的framework中
lipo -create ${build_dir_tem}/${framework_config}-iphonesimulator/${target_name}.framework/${target_name} ${build_dir_tem}/${framework_config}-iphoneos/${target_name}.framework/${target_name} -output ${framework_target_dir}/${target_name}.framework/${target_name}

#删除无用的三方bundle
find ${framework_target_dir}/${target_name}.framework -maxdepth 1 -name "*.bundle" -not -name "${target_name}*.bundle" | xargs rm -rf
#自动打开文件夹
open $framework_target_dir
}

makeFramework(){
#判断临时文件夹是否存在，存在的话先删除
if [ -d $build_dir_tem ];then
rm -rf $build_dir_tem
fi
# 判断输出文件夹是否存在，存在的话先删除
if [ -d ${framework_target_dir} ];then
rm -rf ${framework_target_dir}
fi

#创建临时文件夹
mkdir -p ${build_dir_tem}
#创建输出文件夹
mkdir -p ${framework_target_dir}

buildWorkSpace

rm -rf $build_dir_tem
}

makeFramework
